package com.boot_reggie.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.boot_reggie.Dao.DishDao;
import com.boot_reggie.Service.DishFlavorService;
import com.boot_reggie.Service.DishService;
import com.boot_reggie.domain.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class Dishserviceimpl extends ServiceImpl<DishDao, Dish> implements DishService {
    @Autowired
    private DishDao dishDao;
    @Autowired
    private DishFlavorService service;
    @Autowired
    private CateServiceimpl cateServiceimpl;
    @Transactional
    @Override
    public void saveWithFlavor(DishDto dto) {
        this.save(dto);
        Long disId = dto.getId();
//        菜品口味
        List<DishFlavor> flavors = dto.getFlavors();
        flavors.stream().map((item)->{
            item.setDishId(disId);
            return item;
        }).collect(Collectors.toList());

//        保存菜品口味数据到dishFlavor表

        service.saveBatch(flavors);


    }
    public DishDto getid(Long id){
        System.out.println("============"+id);
        Dish dish = this.getById(id);//先查询菜品信息
        System.out.println(dish);
        DishDto dto = new DishDto();
        BeanUtils.copyProperties(dish,dto);
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,id);
        List<DishFlavor> flavors = service.list(queryWrapper);
        dto.setFlavors(flavors);
        return dto;
    }
    @Transactional
    @Override
    public void updateWithFlavor(DishDto dishDto) {

        this.updateById(dishDto);

        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        service.remove(queryWrapper);
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item)->{
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        service.saveBatch(flavors);
    }
    @Transactional
    @Override
    public void deleLists(List<Dish> dishList){
        this.updateBatchById(dishList);
    }

    @Override
    public List<CategoryDto> getDishList() {
//        先查找所有菜品分类
        LambdaQueryWrapper<Category> lwq = new LambdaQueryWrapper<>();

//        lwq.eq(Category::getType,1);
//        根据排序
        lwq.orderByAsc(Category::getSort);
//        获取所有分类
        List<Category> listCate = cateServiceimpl.list(lwq);
        List<CategoryDto> dishDtos = new ArrayList<>();
        for (Category category : listCate) {
            CategoryDto categoryDto = new CategoryDto();
            BeanUtils.copyProperties(category,categoryDto);
            dishDtos.add(categoryDto);
        }

        /*遍历获取每个分类的菜品*/
        for (CategoryDto dishDto : dishDtos) {
//            菜品分类

            LambdaQueryWrapper<Dish> lb = new LambdaQueryWrapper<>();
            lb.eq(Dish::getIsDeleted,0); //只要正在售卖
            lb.eq(Dish::getStatus,1);
            List<DishDto> dto  = new ArrayList<>();
            lb.eq(dishDto.getId() != null,Dish::getCategoryId,dishDto.getId());
            List<Dish> dishDtos1 = this.list(lb);
            for (Dish dish : dishDtos1) {
                DishDto dishDto1 = new DishDto();
                BeanUtils.copyProperties(dish,dishDto1);
                LambdaQueryWrapper<DishFlavor> lwq1 = new LambdaQueryWrapper<>();
                lwq1.eq(dish.getId() != null,DishFlavor::getDishId,dish.getId());
                List<DishFlavor> flavors = service.list(lwq1);
                dishDto1.setFlavors(flavors);
                dto.add(dishDto1);
            }





            dishDto.setDishDtos(dto);
        }




//
//        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(Dish::getStatus,1);

        return dishDtos;
    }
}
